package com.cloudsigns.factorymanager2.produce.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;

import java.util.ArrayList;
import java.util.List;

public class ProduceAddWorkpartAdapter extends BaseAdapter {
    private Context context;
    private List<WorkPartBean> datas = new ArrayList<WorkPartBean>();
    private LayoutInflater inflater;
    private String TAG = "";
    private int selectedRbPosition = -1;


    public ProduceAddWorkpartAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = tag;
    }

    public void setData(List<WorkPartBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public WorkPartBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_produce_add_workpart, null);
            holder.cb_add_process = convertView.findViewById(R.id.cb_add_process);
            holder.tv_process_name = convertView.findViewById(R.id.tv_process_name);
            holder.rb_last_process = convertView.findViewById(R.id.rb_last_process);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final WorkPartBean productBean = datas.get(position);

        holder.tv_process_name.setText(productBean.getWorkPartName());
        holder.rb_last_process.setBackgroundResource(productBean.isChecked && productBean.isLastWorkPart() ? R.mipmap.icon_radio_bg_yes : R.mipmap.icon_radio_no);
        holder.rb_last_process.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (!datas.get(position).isLastWorkPart()) {
                    if (!datas.get(position).isChecked) {
                        datas.get(position).setLastWorkPart(  false);
                        //toa
                        return;
                    }
                    for (WorkPartBean pb : datas) {
                        if (pb != datas.get(position)) {
                            pb.setLastWorkPart(  false);
                        }
                    }
                    selectedRbPosition = position;
                    datas.get(position).setLastWorkPart(  true);

                    notifyDataSetChanged();
                } else {
                    datas.get(position).setLastWorkPart(  false);
                    notifyDataSetChanged();
                }
            }
        });

        holder.productBean = productBean;
        holder.cb_add_process.setChecked(productBean.isChecked);
        holder.cb_add_process.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!holder.productBean.isChecked) {
                    holder.productBean.isChecked = true;
                } else {
                    holder.productBean.isChecked = false;
                    holder.productBean.setLastWorkPart(  false);
                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        CheckBox cb_add_process;
        TextView tv_process_name;
        ImageView rb_last_process;
        WorkPartBean productBean;
    }

}
